import React, {useEffect, useState} from 'react';
import axios from 'axios';
import {Button, notification} from 'antd'
import NewsPublish from '../../../components/publish-manage/NewsPublish'
const Published = () => {
    const [dataSource, setdataSource] = useState([]);
    const { username, roleId, region } = JSON.parse(localStorage.getItem('token'))
    useEffect(() => {
        axios.get(`/news?publishState=2&_expand=category`).then(res=>{
            const list = res.data
            setdataSource(roleId===1?list:roleId===2?[
                ...list.filter(item=>item.author===username),
                ...list.filter(item=>item.region===region && item.roleId === 3)
            ]:[
                ...list.filter(item=>item.author===username)
            ])
            
        })
    }, [username, roleId, region])
        // 下线按钮
    const handlepublished = (id)=>{
        setdataSource(dataSource.filter(item=>item.id!==id))
        axios.patch(`/news/${id}`,{
            publishState: 3,
        }).then(res=>{
            notification.info({
                message: `删除成功`,
                description:`您可以去[已下线]中查看您下线的新闻`,
                placement:'bottomRight',
              });
        })
    }
    return (
        <div>
            <NewsPublish dataSource={dataSource} 
            button={(id)=><Button danger onClick={()=>handlepublished(id)}>
                下线
                </Button>} ></NewsPublish>
        </div>
    );
};

export default Published;